package io.github.talelin.latticy.controller.v1;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.service.CUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import io.github.talelin.latticy.model.CUser;
import io.github.talelin.latticy.vo.PageResponseVO;

import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;

import org.springframework.web.bind.annotation.RestController;

/**
* @author tanrentao
* @since 2021-08-12
*/
@Validated
@RestController
@RequestMapping("/v1/user")
@PermissionModule("C端用户")
public class CUserController {

    @Autowired
    private CUserService userService;


    @GetMapping("/{id}")
    @PermissionMeta("通过id查询C端用户信息")
    public CUser get(@PathVariable(value = "id") @Positive(message = "{id}") Integer id) {
        return userService.getParsedUserById(id);
    }

    @GetMapping("/page")
    @PermissionMeta("分页查询C端用户列表")
    public PageResponseVO<CUser> page(
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}") Integer count) {
        IPage<CUser> paging = userService.getUserByPage(count, page);
        return PageUtil.build(paging);
    }


    @GetMapping("/search")
    @PermissionMeta("搜索C端用户列表数据")
    public PageResponseVO<CUser> search(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam(name = "keyword") String keyword) {
        IPage<CUser> paging = userService.search(page, count, keyword);
        return PageUtil.build(paging);
    }


}
